﻿using StudentManagement.IService;
using StudentManagement.Model.Entitys;
using StudentManagement.Tools;
using StudentManagement.Util.ValidationRules;
using StudentManagement.ViewModel;
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;

namespace StudentManagement.Views.Student
{
    /// <summary>
    /// AddWindow.xaml 的交互逻辑
    /// </summary>
    public partial class AddWindow : Window
    {
        #region 私有属性及构造函数

        private readonly IClassesService _classesService;
        private readonly IStudentService _studentService;
        public AddWindow(
            IClassesService classesService,
            IStudentService studentService)
        {
            _classesService = classesService;
            _studentService = studentService;

            InitializeComponent();

        }

        private readonly StudentViewModel vm = new StudentViewModel();

        /// <summary>
        /// 双向绑定
        /// </summary>
        private void TwoWayBinding()
        {
            var numberBinding = new Binding(nameof(vm.Number)) { Source = vm };
            numberBinding.ValidationRules.Add(new RequireValidation("请输入学号"));
            numberBinding.ValidationRules.Add(new MaxLengthValidation(50, "学号最长只能输入五十个字符"));
            txtNumber.SetBinding(TextBox.TextProperty, numberBinding);

            var nameBinDing = new Binding(nameof(vm.Name)) { Source = vm };
            nameBinDing.ValidationRules.Add(new RequireValidation("请输入姓名"));
            nameBinDing.ValidationRules.Add(new MaxLengthValidation(10, "名称最长只能输入十个字符"));
            txtName.SetBinding(TextBox.TextProperty, nameBinDing);

            vm.Sex = 1;//默认是男
            rbtnSexMan.SetBinding(RadioButton.IsCheckedProperty, new Binding(nameof(vm.Sex)) { Source = vm, Converter = new ByteConverter(), ConverterParameter = 1 });
            rbtnSexGirl.SetBinding(RadioButton.IsCheckedProperty, new Binding(nameof(vm.Sex)) { Source = vm, Converter = new ByteConverter(), ConverterParameter = 0 });

            var ageBinding = new Binding(nameof(vm.Age)) { Source = vm };
            ageBinding.ValidationRules.Add(new RequireValidation("请输入年龄"));
            ageBinding.ValidationRules.Add(new IntegerValidation("年龄格式错误"));
            ageBinding.ValidationRules.Add(new MaxNumericalValidation(byte.MaxValue, "年龄超过最大范围"));
            ageBinding.ValidationRules.Add(new MinNumericalValidation(1, "年龄不能为零或负数"));
            txtAge.SetBinding(TextBox.TextProperty, ageBinding);

            dpDateBirth.SetBinding(DatePicker.SelectedDateProperty, new Binding(nameof(vm.DateBirth)) { Source = vm, Converter = new DateTimeConver() });

            var classesBinding = new Binding(nameof(vm.Classes)) { Source = vm };
            classesBinding.ValidationRules.Add(new RequireValidation("请选择班级"));
            cbClasses.SetBinding(ComboBox.SelectedItemProperty, classesBinding);
        }

        #endregion

        #region 公开字段

        /// <summary>
        /// 加载父窗口数据
        /// </summary>
        public Action LoadParentData;

        #endregion


        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            cbClasses.ItemsSource = _classesService.QueryAll();

            TwoWayBinding();
        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }


        private void btnSave_Click(object sender, RoutedEventArgs e)
        {

            if (ValidationUtil.CheckIsError(txtNumber, txtName, txtAge, cbClasses)) return;

            _studentService.Insert(AutoMapperHelp.Map<StudentViewModel, StudentEntity>(vm));

            LoadParentData?.Invoke();

            this.Close();
        }
    }
}
